package net.lahuobao.util;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.xssf.usermodel.*;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * Created by Administrator on 2016/11/11.
 */
public class ExcelExportUtil {
    private final String fullFilename;
    private final String filaname;
    private final String suffix;
    private final HttpServletResponse response;
    private final LinkedHashMap<String, String> header;
    private XSSFWorkbook workbook;
    private XSSFSheet sheet;
    private List<String> keys = new ArrayList<String>();
    private int rowIndex;
    public ExcelExportUtil(String filename, String suffix, LinkedHashMap<String, String> header, HttpServletResponse response) {
        this.filaname = filename;
        this.suffix = suffix;
        this.header = header;
        this.response = response;
        this.fullFilename = filename + "." + suffix;
        init();
    }

    private void init() {
        this.workbook = new XSSFWorkbook();
        this.sheet = this.workbook.createSheet("sheet1");

        List<String> headerList = new ArrayList<String>();
        for(Map.Entry<String, String> entry : header.entrySet()) {
            keys.add(entry.getKey());
            headerList.add(entry.getValue());
        }
        XSSFCellStyle style = workbook.createCellStyle();
        XSSFFont font = workbook.createFont();
        font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
        style.setFont(font);

        XSSFRow row = sheet.createRow(0);
        for(int i=0; i<headerList.size(); i++) {
            XSSFCell cell = row.createCell(i);
            XSSFRichTextString text = new XSSFRichTextString(headerList.get(i));
            cell.setCellStyle(style);
            cell.setCellValue(text);
        }
        this.rowIndex++;
    }

    public void writeData(final List<Map> list) {
        try {
            for(Map<String, Object> data : list){
                XSSFRow row = sheet.createRow(this.rowIndex);
                for(int i=0; i<keys.size(); i++) {
                    XSSFCell cell = row.createCell(i);
                    Object object = data.get(keys.get(i));
                    XSSFRichTextString text = new XSSFRichTextString(object == null ? "" : object.toString());
                    cell.setCellValue(text);
                }
                this.rowIndex++;
            }
        } catch(Exception e) {
            error();
            return;
        }
    }

    public void finish() {
        try {
            this.response.setHeader("Content-Disposition","attachment;filename=\"" + new String(this.fullFilename.getBytes("gb2312"), "ISO8859-1") + "\"");
            this.workbook.write(this.response.getOutputStream());
        } catch(Exception e) {
            error();
            return;
        }
    }

    private void error() {
        try {
            this.response.setContentType("application/json;charset=UTF-8");
            this.response.getWriter().write("{\"msg\":\"导出数据出错，请联系管理员。\"}");
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}
